type callbackFn = (item?: any) => void;
const deFn = (fn: callbackFn, time: number | undefined) => {
  let timer: number | undefined;
  return function (...args: any) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.call(this, ...args);
    }, time);
  };
};
const thFn = (fn: callbackFn, time: number | undefined) => {
  let timer: number | null | undefined = null;
  return function (...args: any) {
    if (!timer) {
      timer = setTimeout(() => {
        fn.call(this, ...args);
        timer = null;
      }, time);
    }
  };
};
const thFnByDate = (fn: callbackFn, time: number) => {
  let pre = new Date().getTime();
  return function (...args: any) {
    const now = new Date().getTime();
    if (pre + time <= now) {
      fn.call(this, ...args);
      pre = now;
    }
  };
};

export { deFn, thFn, thFnByDate };
